package com.yonyou.pmclouds.plantemplate.rmiimpl;

import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateItemVO;
import com.yonyou.pmclouds.plantemplate.entity.ScheduleTemplateVO;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateHandleService;
import com.yonyou.pmclouds.plantemplate.rmiitf.IScheduleTemplateItemProcessor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 计划模板任务项迭代处理类
 * @Author: zhangzhonghao
 * @Date: 2020-03-06
 */

@Service
public class ScheduleTemplateHandleServiceImpl implements IScheduleTemplateHandleService {

    private List<IScheduleTemplateItemProcessor> iteratorBeforeProcessorList = new ArrayList<>();

    private List<IScheduleTemplateItemProcessor> iteratorAfterProcessorList = new ArrayList<>();

    @Override
    public void handle(ScheduleTemplateVO templateVO, ScheduleTemplateVO origTemplateVO) throws BusinessException {

        ScheduleTemplateItemVO[] templateItemVOS = templateVO.getScheduleTemplateItemVOS();
        if (templateItemVOS == null || templateItemVOS.length <= 0) {
            throw new BusinessException("计划模板明细项不允许为空！");
        }


        for (ScheduleTemplateItemVO templateItemVO : templateItemVOS) {
            iteratorHandle(templateVO, templateItemVO, null, templateItemVO);
        }

        init();
    }

    @Override
    public void addIteratorBeforeProcessor(IScheduleTemplateItemProcessor itemProcessor) {
        iteratorBeforeProcessorList.add(itemProcessor);
    }

    @Override
    public void addIteratorAfterProcessor(IScheduleTemplateItemProcessor itemProcessor) {
        iteratorAfterProcessorList.add(itemProcessor);
    }

    private void iteratorHandle(ScheduleTemplateVO templateVO, ScheduleTemplateItemVO rootParentItemVO, ScheduleTemplateItemVO parentItemVO,
                                ScheduleTemplateItemVO childItemVO) throws BusinessException {

        handleProcessorList(iteratorBeforeProcessorList, templateVO, rootParentItemVO, parentItemVO, childItemVO);

        if(childItemVO.getChildren() != null && childItemVO.getChildren().length > 0){

            for(ScheduleTemplateItemVO childItemVOTemp : childItemVO.getChildren()){
                iteratorHandle(templateVO, rootParentItemVO, childItemVO, childItemVOTemp);
            }

        }

        handleProcessorList(iteratorAfterProcessorList, templateVO, rootParentItemVO, parentItemVO, childItemVO);
    }

    private void handleProcessorList(List<IScheduleTemplateItemProcessor> itemProcessors, ScheduleTemplateVO templateVO,
                                     ScheduleTemplateItemVO rootParentItemVO, ScheduleTemplateItemVO parentItemVO,
                                     ScheduleTemplateItemVO childItemVO) throws BusinessException {
        if(itemProcessors.size() <= 0){
            return;
        }
        for (IScheduleTemplateItemProcessor itemProcessor : itemProcessors) {
            itemProcessor.process(templateVO, rootParentItemVO, parentItemVO, childItemVO);
        }
    }

    private void init(){
        iteratorAfterProcessorList = new ArrayList<>();
        iteratorBeforeProcessorList = new ArrayList<>();
    }

}
